package com.luffer.springboot.demo.mqbasis.mqClient;

import com.luffer.springboot.demo.mqbasis.mqServer.BrokerServer;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Author: dengkaibing
 * @Description:
 * @Date: 2021/8/3 15:07
 * @company 翼支付
 */
@Slf4j
public class MqClient {

    public static void produce(String msg) throws IOException {
        Socket socket=new Socket(InetAddress.getLocalHost(), BrokerServer.SERVICE_PORT);


        try(PrintWriter out=new PrintWriter(socket.getOutputStream())){
            out.println(msg);
            out.flush();
        }
    }
    public static String consume() throws Exception{
        Socket socket = new Socket(InetAddress.getLocalHost(), BrokerServer.SERVICE_PORT);
    try (BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())
    ); PrintWriter out = new PrintWriter(socket.getOutputStream())
    ){
        out.println("CONSUME");
        out.flush();
        return in.readLine();

    }
    }

    public static void main(String[] args) throws Exception {
         MqClient mqClient=new MqClient();
         mqClient.produce("Hello world1");
         log.info("消费的消息：{}",mqClient.consume());
    }

}
